#include <cstring>
#include <iostream>

#define x first
#define y second

using namespace std;

const int N = 160;

typedef pair<int, int> PII;

int n, m;
char g[N][N];
PII q[N * N];
int dist[N][N];

int bfs() {
    int sx = 0, sy = 0;
    for (int i = 0; i < n; ++ i)
        for (int j = 0; j < m; ++ j) 
            if (g[i][j] == 'K') sx = i, sy = j;
            
    int hh = 0, tt = 0;
    q[0] = {sx, sy};
    
    memset(dist, -1, sizeof dist);
    dist[sx][sy] = 0;
    
    int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
    int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
    while (hh <= tt) {
        PII t = q[hh ++ ];
        
        for (int i = 0; i < 8; ++ i) {
            int a = t.x + dx[i], b = t.y + dy[i];
            
            if (a < 0 || a >= n || b < 0 || b >= m) continue;
            if (g[a][b] == '*') continue;
            if (dist[a][b] != -1) continue;
            if (g[a][b] == 'H') return dist[t.x][t.y] + 1;
            
            q[ ++ tt] = {a, b};
            dist[a][b] = dist[t.x][t.y] + 1;
        }
    }
        
}

int main() {
    cin >> m >> n;
    
    for (int i = 0; i < n; ++ i) scanf("%s", g[i]);
    
    cout << bfs() << endl;
    
    return 0;
}